package com.exp3;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.awt.event.ActionEvent;

/**
 * <p>Title: Screen</p>
 * <p>Description: 展示数据的界面</p>
 * @author 詹金浩
 * @date 2018年11月26日
 */
public class ScreenGetDataWithSocket extends JFrame{

	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel tm;
	
	private static Map<String, PortGuidance> map = new LinkedHashMap<>();
	private static BlockingDeque<String> list = new LinkedBlockingDeque<>();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ScreenGetDataWithSocket frame = new ScreenGetDataWithSocket();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public ScreenGetDataWithSocket() {
		setTitle("\u822A\u73ED\u663E\u793A\u7CFB\u7EDF\u4EFF\u771F");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(250, 200, 800, 430);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		final JButton startButton = new JButton("\u542F\u52A8");
		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new ReadFromSocket(list).start();
				new HandleData(list, map).start();
				new ShowData(tm, map, false).start();
				startButton.setEnabled(false);
			}
		});
		panel.add(startButton);
		JButton stopButton = new JButton("\u505C\u6B62");
		stopButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		panel.add(stopButton);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		tm=new DefaultTableModel(
			new Object[][] { },
			new String[] {"航班号", "共享航班号", "经停站", "目的地", "预计起飞", "实际起飞", "航班状态", "值班柜台"}
		);
		table.setModel(tm);
		//设定表格数据的自动排序
		RowSorter<DefaultTableModel> rowSorter=new TableRowSorter<DefaultTableModel>(tm);
		table.setRowSorter(rowSorter);
		scrollPane.setViewportView(table);
	}
}